package com.example.module_mine.ui

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.module_mine.R
import com.example.module_mine.databinding.ActivityMyapplyDetailBinding
import com.example.module_mine.ui.adapter.ReviewAdapter
import com.example.module_mine.ui.adapter.ReviewItem
import com.google.android.material.tabs.TabLayout
import retrofit2.http.Path

@Route(path = "/module_mine/MyapplyDetailActivity")
class MyapplyDetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_myapply_detail)
        val binding = ActivityMyapplyDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)


        // 初始化 Toolbar
//        val toolbar = binding.toolbar
//        setSupportActionBar(toolbar)
//        supportActionBar?.setDisplayHomeAsUpEnabled(true)
//        toolbar.setNavigationOnClickListener {
//            onBackPressed()
//        }

        // 初始化 TabLayout 和 RecyclerView
        val tabLayout = binding.tabLayout
        val recyclerView = binding.recyclerView

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                // 切换标签页时更新 RecyclerView 数据
                updateRecyclerViewData(tab?.position ?: 0)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {}
            override fun onTabReselected(tab: TabLayout.Tab?) {}
        })

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = ReviewAdapter()

        // 初始化 SearchView
        val searchView = binding.searchView
        searchView.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                // 执行搜索操作
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                // 实时更新搜索结果
                return true
            }
        })

        // 默认加载全部数据
//        updateRecyclerViewData(0)
        val adapter = recyclerView.adapter as? ReviewAdapter
        adapter?.setData(getReviewItemsForTab(0))
    }

    private fun updateRecyclerViewData(tabPosition: Int) {
        // 根据 tabPosition 更新 RecyclerView 数据
//        val adapter = recyclerView.adapter as? ReviewAdapter
//        adapter?.setData(getReviewItemsForTab(tabPosition))
    }

    private fun getReviewItemsForTab(tabPosition: Int): List<ReviewItem> {
        // 根据 tabPosition 返回相应的数据
        return when (tabPosition) {
            0 -> getAllReviewItems() // 全部
            1 -> getParkingReviewItems() // 车位审核
            2 -> getVisitorReviewItems() // 来访审核
            3 -> getNewsReviewItems() // 新闻审核
            else -> emptyList()
        }
    }

    private fun getAllReviewItems(): List<ReviewItem> {
        // 返回所有审核项数据
        return listOf(
            ReviewItem(
                id = 1,
                title = "访客预约审核",
                detail = "张先生 - A栋1单元1201\n访客：李女士\n来访时间：2024-01-20 14:00-16:00\n来访事由：探亲访友",
                status = "待审核"
            ),
            ReviewItem(
                id = 2,
                title = "车位转让审核",
                detail = "王先生 - B栋2单元1503\n车位号：B区-123\n转让期限：2024-01-20 至 2024-07-20\n转让原因：临时外出",
                status = "已通过"
            ),
            ReviewItem(
                id = 3,
                title = "园区公告审核",
                detail = "物业管理处\n标题：关于春节期间物业服务安排的通知\n发布时间：2024-01-19 10:30\n内容：春节期间物业服务时间调整...",
                status = "已拒绝"
            )
        )
    }

    private fun getParkingReviewItems(): List<ReviewItem> {
        // 返回车位审核项数据
        return listOf(
            ReviewItem(
                id = 2,
                title = "车位转让审核",
                detail = "王先生 - B栋2单元1503\n车位号：B区-123\n转让期限：2024-01-20 至 2024-07-20\n转让原因：临时外出",
                status = "已通过"
            )
        )
    }

    private fun getVisitorReviewItems(): List<ReviewItem> {
        // 返回来访审核项数据
        return listOf(
            ReviewItem(
                id = 1,
                title = "访客预约审核",
                detail = "张先生 - A栋1单元1201\n访客：李女士\n来访时间：2024-01-20 14:00-16:00\n来访事由：探亲访友",
                status = "待审核"
            )
        )
    }

    private fun getNewsReviewItems(): List<ReviewItem> {
        // 返回新闻审核项数据
        return listOf(
            ReviewItem(
                id = 3,
                title = "园区公告审核",
                detail = "物业管理处\n标题：关于春节期间物业服务安排的通知\n发布时间：2024-01-19 10:30\n内容：春节期间物业服务时间调整...",
                status = "已拒绝"
            )
        )
    }
}